package com.jianguozh.difs.support.api.ops;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ArrayUtil;
import com.jianguozh.difs.common.bean.R;
import com.jianguozh.difs.framework.base.BaseController;
import com.jianguozh.difs.framework.constant.enums.ClientEnum;
import com.jianguozh.difs.framework.utils.SecurityUtils;
import com.jianguozh.difs.support.api.ops.dto.ParamDto;
import com.jianguozh.difs.support.service.component.prototypeparam.PrototypeParam;
import com.jianguozh.difs.support.service.base.entity.Param;
import com.jianguozh.difs.support.service.base.service.IParamService;
import com.jianguozh.difs.support.service.base.vo.GroupVo;
import com.jianguozh.difs.support.service.base.vo.ParamVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

/**
 * <p>
 * 参数配置 前端控制器
 * </p>
 *
 * @author hanrs
 * @since 2022-03-17
 */
@RestController
@RequestMapping("/param")
public class ParamController extends BaseController {

    @Autowired
    private IParamService paramService;

    /**
     * 由key获取对象
     */
    @GetMapping("")
    public R<ParamVo> get(@RequestParam("paramKey") String paramKey, @RequestParam("appId") String appId) {
        ClientEnum clientEnum = ClientEnum.fromAppId(appId);
        ParamVo paramVo = paramService.getVoByKey(paramKey, SecurityUtils.getLoginUser().getOrgId());
        if (null != paramVo && ArrayUtil.indexOf(paramVo.getActAtClientEnum(), clientEnum) > -1) {
            return success(paramVo);
        } else {
            return failed("查无此参数.");
        }
    }

    /**
     * 更新参数值
     *
     * @param paramDto
     * @return
     */
    @PutMapping("")
    public R<Boolean> modify(@RequestBody @Validated ParamDto paramDto) {

        String paramKey = paramDto.getParamKey();
        PrototypeParam prototypeParam = paramService.getPrototypeByKey(paramKey);
        if (null == prototypeParam) {
            return failed("查无此参数.");
        }

        String paramValue = paramDto.getParamValue();
        String paramDefaultValue = prototypeParam.getParamDefaultValue();
        Param param = paramService.getByKey(paramKey, SecurityUtils.getLoginUser().getOrgId());
        if (paramDefaultValue.equalsIgnoreCase(paramValue) && null != param) {
            paramService.removeById(param.getId());
        } else if (!paramDefaultValue.equalsIgnoreCase(paramValue) && null != param) {
            if (!param.getParamValue().equalsIgnoreCase(paramValue)) {
                param.setParamValue(paramValue);
                paramService.updateById(param);
            }
        } else if (!paramDefaultValue.equalsIgnoreCase(paramValue) && null == param) {
            Param newParam = new Param();
            BeanUtil.copyProperties(paramDto, newParam);
            newParam.setActAtOrgId(SecurityUtils.getLoginUser().getOrgId());
            paramService.save(newParam);
        }

        return success(true);
    }

    /**
     * 设为默认值
     *
     * @return
     */
    @PutMapping("/to-default/{paramKey}")
    public R<Boolean> setToDefault(@PathVariable String paramKey) {
        PrototypeParam prototypeParam = paramService.getPrototypeByKey(paramKey);
        if (null == prototypeParam) {
            return failed("查无此参数.");
        }

        Param param = paramService.getByKey(paramKey, SecurityUtils.getLoginUser().getOrgId());
        if (null != param) {
            paramService.removeById(param.getId());
        }
        return success(true);
    }

    /**
     * 获取端的参数组集合
     *
     * @param appId
     * @return
     */
    @GetMapping("/groups")
    public R<Set<GroupVo>> getGroups( @RequestParam("appId") String appId) {
        ClientEnum clientEnum = ClientEnum.fromAppId(appId);
        return success(paramService.getGroupVos(clientEnum, SecurityUtils.getLoginUser().getOrgId()));
    }

    /**
     * 获取端参数组
     *
     * @param appId
     * @param groupKey
     * @return
     */
    @GetMapping("group")
    public R<GroupVo> getGroup(@RequestParam("appId") String appId, @RequestParam("groupKey") String groupKey) {
        ClientEnum clientEnum = ClientEnum.fromAppId(appId);
        GroupVo groupVo = paramService.getGroupVo(groupKey, SecurityUtils.getLoginUser().getOrgId());
        return ArrayUtil.indexOf(groupVo.getSetByClientEnum(), clientEnum) > -1 ? success(groupVo) : failed("查无此参数组");
    }

}
